package org.jpro.modules.system.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jpro.common.jpa.Specifications;
import org.jpro.modules.system.dao.LogRepository;
import org.jpro.modules.system.entity.Log;
import org.jpro.modules.system.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <P>功能描述 : 日志service实现 </P>
 * <P>File name : LogServiceImpl </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017/3/3 下午11:59</P>
 */
@Service
@Transactional(readOnly = true)
public class LogServiceImpl implements ILogService {

    @Autowired
    private LogRepository logRepository;

    @Override
    public Page<Log> search(String operationCode, String description, Date gtCreateDate, Date ltCreateDate, PageRequest pageRequest) {
        Specification<Log> specification = Specifications.<Log> and()
                .like(StringUtils.isNoneBlank(operationCode), "operationCode", "%" + operationCode + "%")
                .like(StringUtils.isNoneBlank(description), "description", "%" + description + "%")
                .ge(gtCreateDate != null, "createDate", gtCreateDate).le(ltCreateDate != null, "createDate", ltCreateDate).build();

        return logRepository.findAll(specification, pageRequest);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        logRepository.delete(id);
    }

    @Override
    @Transactional
    public void deleteLog(List<Integer> ids) {
        logRepository.deleteByIds(ids);
    }
}
